package whu.o2o.util;







import whu.o2o.BaseActivity;

import whu.o2o.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ImeUtil {
	static Toast toast = null;

	/**
	 * �Զ���Toast��ʽ mcontext:�����ڶ��� text:�ı���ʾ���� duration ��ʾ��ͣ��ʱ�� ����λ:����
	 */
	public static void showToast(BaseActivity mcontext, int text, int duration) {
		if (toast != null) {
			toast.cancel();
			toast = null;
		}
		View toastRoot = mcontext.getLayoutInflater().inflate(R.layout.my_toast, null);
		toast = new Toast(mcontext);
		toast.setView(toastRoot);
		toast.setDuration(duration);
		toast.setGravity(Gravity.CENTER, 0, 0);
		TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
		tv.setText(text);
		toast.show();
	}

	public static void showToast(BaseActivity mcontext, String text, int duration) {
		if (toast != null) {
			toast.cancel();
			toast = null;
		}
		View toastRoot = mcontext.getLayoutInflater().inflate(R.layout.my_toast, null);
		toast = new Toast(mcontext);
		toast.setView(toastRoot);
		toast.setDuration(duration);
		toast.setGravity(Gravity.CENTER, 0, 0);
		TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
		tv.setText(text);
		toast.show();
	}

	public static void showToast(BaseActivity mcontext, int resid, String text, int duration) {
		if (toast != null) {
			toast.cancel();
			toast = null;
		}
		View toastRoot = mcontext.getLayoutInflater().inflate(R.layout.my_toast, null);
		toast = new Toast(mcontext);
		toast.setView(toastRoot);
		toast.setDuration(duration);
		toast.setGravity(Gravity.CENTER, 0, 0);
		TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
		tv.setText(text);
		ImageView img = (ImageView) toastRoot.findViewById(R.id.loadingImageView);
		img.setBackgroundResource(resid);
		toast.show();
	}

	public static void showToast(Activity mcontext, int text, int duration) {
		if (toast != null) {
			toast.cancel();
			toast = null;
		}
		View toastRoot = mcontext.getLayoutInflater().inflate(R.layout.my_toast, null);
		toast = new Toast(mcontext);
		toast.setView(toastRoot);
		toast.setDuration(duration);
		toast.setGravity(Gravity.CENTER, 0, 0);
		TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
		tv.setText(text);
		toast.show();
	}

	/**
	 * �Ƿ���Ȩĳһ����ģ��
	 * 
	 * @param sn
	 *            ĳһ����ģ�����
	 * @param rightsId
	 *            Ȩ���б�
	 * @return
	 */
	public static boolean isAuthorize(String sn, String rightsId) {
		boolean tag = false;
		try {
			String[] a = rightsId.split(",");
			for (String rights : a) {
				if (rights.equals(sn)) {
					tag = true;
					break;
				}
			}
		} catch (NullPointerException e) {
		}
		return tag;
	}

	@SuppressWarnings("static-access")
	public static void hideIme(Activity context) {
		if (context == null)
			return;
		final View v = context.getWindow().peekDecorView();
		if (v != null && v.getWindowToken() != null) {
			InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
	}

	@SuppressWarnings("static-access")
	public static boolean isImeShow(Context context) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
		return imm.isActive();
	}

	public static void showIme(Context context, EditText et) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(et, InputMethodManager.SHOW_FORCED);
	}

	/**
	 * compute Sample Size
	 * 
	 * @param options
	 * @param minSideLength
	 * @param maxNumOfPixels
	 * @return
	 */
	public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}

		return roundedSize;
	}

	/**
	 * compute Initial Sample Size
	 * 
	 * @param options
	 * @param minSideLength
	 * @param maxNumOfPixels
	 * @return
	 */
	private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;
		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? minSideLength : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

		if (upperBound < lowerBound) {
			// return the larger one when there is no overlapping zone.
			return lowerBound;
		}
		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}
}